package com.spider.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.domain.Datasource;
import com.spider.domain.Spider;
import com.spider.dao.SpiderDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spider.domain.Start;
import com.spider.util.SpiderYangGuang;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HQJ
 * @since 2021-02-03
 */
@Service
public class SpiderServiceI extends ServiceImpl<SpiderDao, Spider> {

    @Resource(name = "datasourceServiceI")
    private DatasourceServiceI dsi;


    public Map<String,List<String>> test(Spider s){
        Map<String,Object> map = new HashMap<>();

        String rules = s.getRules();
        if(rules!=null){
            //把字符串转换为json数组
            JSONArray js  = JSON.parseArray(rules);
            for(int i=0;i<js.size();i++){
                JSONObject jo = js.getJSONObject(i);
                if(jo.containsKey("variable")){
                    JSONObject v = jo.getJSONObject("variable");
                    JSONArray vName = v.getJSONArray("variableName");
                    JSONArray vValue = v.getJSONArray("variableValue");
                    SpiderYangGuang.getContent(map,vName,vValue);
                }else if(jo.containsKey("output")){
                    //$content/#loop/$text,$href,$src
                    //获取输出的json对象
                    JSONObject o = jo.getJSONObject("output");
                    //获取表格的内容
                    JSONArray oName = o.getJSONArray("outputName");
                    JSONArray oValue = o.getJSONArray("outputValue");
                    JSONArray oColumn = o.getJSONArray("outputColumn");
                    Map<String,List<String>> m = SpiderYangGuang.output(map,oName,oValue,oColumn);
                    return m;
                }else if(jo.containsKey("contentVar")){
                    int socket = jo.getInteger("socket");
                    int connect = jo.getInteger("connect");
                    String encoding = jo.getString("encoding");
                    String method = jo.getString("method");
                    JSONObject v = jo.getJSONObject("contentVar");

                    JSONArray vName = v.getJSONArray("variableName");
                    JSONArray vValue = v.getJSONArray("variableValue");

                    Map<String,List<String>> m = SpiderYangGuang.output(map,vName,vValue,null);

                    List<String> list = m.get(vName.getString(0));
                    List<String> hlist = new ArrayList<>();
                    int a = list.size()>10?10:list.size();
                    for(String url : list) {
                        String uri = s.getUri() + url;
                        String html = SpiderYangGuang.sendUri(uri, socket, connect, encoding, method);
                        hlist.add(html);
                        a--;
                        if(a<=0){
                            break;
                        }
                        //System.out.println(html);
                    }
                    map.put("html",hlist);


                }else{
                    String uri = jo.getString("url");
                    int socket = jo.getInteger("socket");
                    int connect = jo.getInteger("connect");
                    String encoding = jo.getString("encoding");
                    String method = jo.getString("method");
                    String html = SpiderYangGuang.sendUri(uri,socket,connect,encoding,method);
                    map.put("html",html);
                }
            }
        }

        return null;
    }


    /**
     * 判断是否保存到数据库
     * 获取保存列名<list>
     * 获取保存的数据库
     * 获取保存表的名称
     *  map<String,Object>
     * @param s
     * @return
     */
    public Map<String,Object> getColumnMap(Spider s){
        Map<String,Object> cmap = new HashMap<>();
        String rules = s.getRules();
        if(rules!=null){
            JSONArray js = JSON.parseArray(rules);
            for(int i=0;i<js.size();i++){
                JSONObject jb = js.getJSONObject(i);
                if(jb.containsKey("output")){
                    JSONObject o = jb.getJSONObject("output");
                    //列
                    JSONObject ja = jb.getJSONObject("style");
                    String style = ja.getString("database");
                    Map<String,String> cm = new HashMap<>();
                    if (style!=null&&"database".equals(style)) {
                        JSONArray oColumn = o.getJSONArray("outputColumn");
                        JSONArray oName = o.getJSONArray("outputName");
                        for (int y = 0; y < oColumn.size(); y++) {
                            String key = oName.getString(y);
                            String value = oColumn.getString(y);
                            cm.put(key,value);
                        }
                        cmap.put("column", cm);
                        //数据库
                        cmap.put("database", jb.getString("database"));
                        //数据表
                        cmap.put("tableName", jb.getString("tableName"));
                    }

                }
            }
        }
        return cmap;
    }


    public boolean saveData(Map<String,Object> cmap,Map<String,List<String>> map){
        //获取数据库的连接
        try{
            Datasource ds = dsi.getById(Integer.parseInt(cmap.get("database")+""));
            Class.forName(ds.getDriver());
            Connection conn = DriverManager.getConnection(ds.getUrl(),ds.getUsername(),ds.getPassword());
            if(conn!=null){

                Statement st = conn.createStatement();
                StringBuffer sql = new StringBuffer("insert into "+cmap.get("tableName"));
                //拼接列名
                Map<String,String> cm = (Map<String,String>)cmap.get("column");
                if(cm.size()>0){
                    sql.append("(");
                    boolean flat = false;
                    for(String key:cm.keySet()){
                        if(flat){
                            sql.append(",");
                        }
                        sql.append(cm.get(key));
                        flat = true;
                    }
                    sql.append(")");
                }
                //拼接数据
                if(map.size()>0){
                    sql.append(" values ");
                    int size = 0;
                    for(String key : map.keySet()){
                        size = map.get(key).size();
                    }
                    boolean flat = false;
                    for(int i=0;i<size;i++){
                        if(flat){
                            sql.append(",");
                        }
                        sql.append("(");
                        boolean b = false;
                        for(String key:map.keySet()){
                            List<String> vs = map.get(key);
                            sql.append(b?",":"");
                            sql.append("'"+vs.get(i).replaceAll("'","&apos;")+"'");
                            b = true;
                        }
                        sql.append(")");
                        flat = true;
                    }
                }
                //System.out.println("sql="+sql);
                int x = st.executeUpdate(sql.toString());
                st.close();
                conn.close();
                return x>0?true:false;
            }



        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
